package com.xsherl.leetcode.solution;

public class EngthOfLastWord {

    /**
     * 从尾部开始查找，忽略尾部空格
     */
    public int lengthOfLastWord(String s) {
        if (s.length() == 0){
            return 0;
        }
        int i = s.length() - 1;
        while (i >= 0 && s.charAt(i) == ' '){
            i--;
        }
        int count = 0;
        while (i >= 0 && s.charAt(i) != ' '){
            i--;
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        String s = "Hello World    ";
        int i = new EngthOfLastWord().lengthOfLastWord(s);
        System.out.println(i);
    }
}
